﻿Imports MySql.Data.MySqlClient

Public Class CKelas
    Inherits CContext

    Private INSERT_COMMAND As String
    Private UPDATE_COMMAND As String
    Private DELETE_COMMAND As String
    Private SELECT_COMMAND As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub simpan(ByVal nama As String, ByVal tingkatan As Integer, ByVal kapasitas As Integer)
        Me.INSERT_COMMAND = "INSERT INTO TBL_KELAS (NAMA,TNGKT,KPSTS) VALUES ('" & nama & "'," & tingkatan & "," & kapasitas & ")"
        MyBase.InsertData(INSERT_COMMAND)
    End Sub

    Public Sub rubah(ByVal nama As String, ByVal tingkatan As Integer, ByVal kapasitas As Integer, ByVal idKelas As Integer)
        Me.UPDATE_COMMAND = "UPDATE TBL_KELAS SET NAMA='" & nama & "', TNGKT=" & tingkatan & ",KPSTS=" & kapasitas & " WHERE ID=" & idKelas
        MyBase.UpdateData(UPDATE_COMMAND)
    End Sub

    Public Sub hapus(ByVal idKelas As Integer)
        'validasi
        If idKelas > 0 Then
            Me.DELETE_COMMAND = "DELETE FROM TBL_KELAS WHERE ID=" & idKelas
            MyBase.DeleteData(DELETE_COMMAND)
        Else
            Throw New Exception
        End If
    End Sub

    Public Function getAll() As DataTable
        Me.SELECT_COMMAND = "SELECT * FROM TBL_KELAS"
        Return MyBase.SelectData(SELECT_COMMAND)
    End Function

    Public Function getByTingakatan(ByVal tingkatan As Integer) As DataTable
        Me.SELECT_COMMAND = "SELECT * FROM TBL_KELAS WHERE TNGKT=" & tingkatan
        Return MyBase.SelectData(SELECT_COMMAND)
    End Function

    Public Function getById(idkelas As Integer) As tbl_kelas
        Dim _res As New tbl_kelas
        MyBase.MySqlConnect.Open()
        Dim SELECT_CMD As MySqlCommand = MyBase.MySqlConnect.CreateCommand
        Dim reader As MySqlDataReader
        With SELECT_CMD
            .CommandText = "SELECT id,nama,tngkt,kpsts FROM TBL_kelas  WHERE ID = " & idkelas
            reader = .ExecuteReader
        End With
        While reader.Read
            _res.id = reader.Item("id")
            _res.nama = reader.Item("nama")
            _res.tngkt = reader.Item("tngkt")
            _res.kpsts = reader.Item("kpsts")
        End While
        reader.Close()
        MySqlConnect.Close()
        Return _res
    End Function

End Class
